package com.webservice.impl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.webservice.dao.Pub;
import com.webservice.dao.PubBaseDao;
import com.webservice.dao.Stockinfodao.IStockInfoDao;
import com.webservice.dao.Stockinfodao.impl.StockInfoDaoImpl;
import com.webservice.service.IBaseService;
import com.webservice.utils.DateTimeTool;
import com.webservice.utils.MyApplicationContextUtil;
import com.webservice.utils.UUIDUtil;
import com.webservice.utils.VOXMLMapping;
import com.webservice.vo.impl.LogInterfaceVO;
import com.webservice.vo.impl.ReturnMsgVo;
import com.webservice.vo.impl.StockinfoVo;

@Service("Q1001")
public class StockinfoService implements IBaseService {
	private static Logger logger = Logger.getLogger(Customer.class);
	@Autowired
	@Qualifier("stockInfoDao")
	private IStockInfoDao stockDao;

	public IStockInfoDao getStockDao() {
		return stockDao;
	}

	public void setStockDao(IStockInfoDao stockDao) {
		this.stockDao = stockDao;
	}

	/***
	 * (非 Javadoc)
	 * <p>
	 * Title: returnData
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param xml
	 * @param baseDao
	 * @param ip
	 * @return
	 * @throws Exception
	 * @see com.webservice.service.IBaseService#returnData(java.lang.String,
	 *      com.webservice.dao.PubBaseDao, java.lang.String)
	 */
	@Override
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String returnData(String xml, PubBaseDao baseDao, String ip)
			throws Exception {
		LogInterfaceVO logvo = null;
		ReturnMsgVo returnMsgVo = new ReturnMsgVo();
		StockinfoVo stockinfoVo = new StockinfoVo();
		String intoUUID = UUIDUtil.getUUID();
		String outUUID = UUIDUtil.getUUID();
		// 成对标志
		String pairflag = DateTimeTool.getDateTimeString(new Date(),"yyyymmddHHmmss:sss").replace(":", "");
		/**
		 * 转换XML到VO逻辑
		 */
		try {
			stockinfoVo = (StockinfoVo) VOXMLMapping.fromXML(xml,
					StockinfoVo.class, null);// xml字符串转VO
			logvo = new LogInterfaceVO(intoUUID,Pub.getCurrTime(),ip, "07",
					stockinfoVo.getPk_store(), "into",pairflag,"0",xml);
			baseDao.insert(logvo);
			logger.debug("XML转换成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.debug("XML转换VO失败");
			returnMsgVo.setResult(1);
			returnMsgVo.setMsg("XML转换VO失败:" + ex.getMessage());
			logvo = new LogInterfaceVO(intoUUID,Pub.getCurrTime(), ip, "07",
					stockinfoVo.getPk_store(), "into", pairflag,"0",xml);
			baseDao.insert(logvo);
			return VOXMLMapping.toXML(returnMsgVo, null, null);// VO转换为XML字符串
		}

		/**
		 * 插入库存表逻辑
		 */
		try {
			stockDao = (StockInfoDaoImpl) MyApplicationContextUtil.getContext()
					.getBean("stockInfoDao");
			int result = stockDao.InsertStockInfo(stockinfoVo);
			if (result > 0) {
				returnMsgVo.setResult(0);
				returnMsgVo.setMsg("库存插入成功");
			} else {
				returnMsgVo.setResult(-1);
				returnMsgVo.setMsg("库存插入失败");
			}
		} catch (Exception ex) {
			returnMsgVo.setResult(-1);
			returnMsgVo.setMsg("库存插入失败：" + ex.getMessage());
			logvo = new LogInterfaceVO(intoUUID,Pub.getCurrTime(), ip, "07",
					stockinfoVo.getPk_store(), "into", pairflag,"0",xml);
			baseDao.insert(logvo);
			return VOXMLMapping.toXML(returnMsgVo, null, null);// VO转换为XML字符串
		}

		/**
		 * 处理成功后返回XML字符串逻辑
		 */
		String returnxml = "";
		try {
			returnxml = VOXMLMapping.toXML(returnMsgVo, null, null);// VO转换为XML字符串
		} catch (Exception ex) {
			logvo = new LogInterfaceVO(intoUUID,Pub.getCurrTime(), ip, "07",
					stockinfoVo.getPk_store(), "into", pairflag,"0",xml);
			baseDao.insert(logvo);
			return VOXMLMapping.toXML(returnMsgVo, null, null);// VO转换为XML字符串
		}
		logger.debug("__________________返回库存结果信息的xml:" + returnxml);
		logvo = new LogInterfaceVO(outUUID,Pub.getCurrTime(), ip, "07",
				stockinfoVo.getPk_store(), "out", pairflag,"1",returnxml);
		logger.debug("__________________插入信息:" + logvo.toString());
		baseDao.insert(logvo);
		logger.debug("插入日志成功");
		Pub.insertDealResultLog(baseDao, outUUID, "WMS", "B2B", "成功", "库存处理成功");
		return returnxml;
	}
}
